package hot100;

import comment.ListNode;

/**
 * @Author: wzp
 * @CreateTime: 2024-09-18  11:42
 * @Description: 判断链表是不是回文链表
 * @Version: 1.0
 */
public class hot_234 {
    public boolean isPalindrome(ListNode head) {
        if(head==null||head.next==null){
            return true;
        }
//        采用快慢指针的方式
        ListNode slow=head;
        ListNode fast=head;
        ListNode p=null;
        ListNode pre=null;
        while(true){
            if(fast==null||fast.next==null){
                break;
            }
            p=slow;
            slow=slow.next;
            fast=fast.next.next;
            p.next=pre;
            pre=p;
        }

        if(fast!=null){
            slow=slow.next;
        }
        while (p!=null&&slow!=null){
            if(p.val!= slow.val){
                return false;
            }
            p=p.next;
            slow=slow.next;
        }
        return true;


    }

}
